package com.nercel.dsj.gksales.util.sms.unicom;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;

/**
 * @author qsx
 * @date 2020-09-04 13:56:22
 */
@Component
public class UnicomSmsUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${unicomSms.url}")
    private String url;

    @Value("${unicomSms.LoginName}")
    private String LoginName;

    @Value("${unicomSms.spPassword}")
    private String spPassword;

    /**
     * 发送短信
     * 
     * @param to         接收短信着的手机号
     * @param templateId 短信模板id
     * @param parameter  模板参数
     */
    public void sendSms(String to, String templateId, String parameter) {
	HttpHeaders headers = new HttpHeaders(); // http请求头
	headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 请求头设置属性
	MessageRequest mr = new MessageRequest();
	mr.setLoginName(LoginName);
	mr.setParameter(parameter);
	mr.setSmsTempletID(templateId);
	mr.setTimestamp(true, null);
	mr.setUserNumber(to);
	mr.setSpPassword(spPassword);
	mr.setVerify(mr.md5HexUpperCase());
	Gson gson = new Gson();
	String json = gson.toJson(mr);
	HttpEntity<String> requestEntity = new HttpEntity<>(json, headers);
	MessageResponse messageResponse = restTemplate.postForObject(url, requestEntity, MessageResponse.class);
	if ("0".equals(messageResponse.getResult())) {
	    // TODO 记录发短信数据到数据库
	}
    }

}
